#!/usr/bin/env python
# -*- coding:utf8 -*-

import os, os.path
import numpy
import math

def dft1(nsamples):
    # use list to do the transformation
    # 使用了复数公式法来计算系数，其中，复数用cosx+isinx来表示。
    # 参考书是奥本海姆的那本，以及《数学分析》第二册那本。
    coefs = calcoef(nsamples)
    amps = []
    for i in coefs:
        amps.append(calamplitue(i))
    return amps


def calcoef(nsamples):
    # calculate the coefficients of each co-waves
    samnum = len(nsamples)
    coefs = []
    
    for i in xrange(samnum):
        curcoef = (0,0)
        for j in xrange(samnum):
            curcoef = fadd(curcoef, fmul(transf(nsamples[j]), calWNnk(samnum, i, j)))
        coefs.append(curcoef)

def calamplitue(f1):
    return 2*math.sqrt(math.pow(f1[0],2)+math.pow(f1[1],2))
        
def calWNnk(N, n, k):
    # cal e**(-j*Pi*n*k/N)
    fre = math.cos(-math.pi*n*k/N)
    fim = math.sin(-math.pi*n*k/N)
    return (fre, fim)

def fadd(f1, f2):
    # define the sum of two f-numbers
    f3real = f1[0] + f2[0]
    f3imag = f1[1] + f2[1]
    f3 = (f3real, f3imag)
    return f3

def fmul(f1, f2):
    # define the multiple of two f-numbers
    # f-number is difined as a two-elements tuple
    f3real = f1[0] * f2[0] - f1[1] * f2[1]
    f3imag = f1[0] * f2[1] + f1[0] * f2[0]
    f3 = (f3real, f3imag)
    return f3
    
def transf(r1):
    return (r1, 0)





 
